var a = require("../../utils/config.js");

Page({
    data: {
        taskId: "",
        taskDetail: {},
        qrImageUrl: "",
        isShow: !1,
        isFavorite: !1,
        shareImageUrl: "",
        canvasWidth: 300,
        canvasHeight: 400
    },
    onLoad: function(a) {
        console.log(a);
        var t = this;
        a.id ? t.setData({
            taskId: a.id
        }) : a.scene && t.setData({
            taskId: decodeURIComponent(a.scene)
        }), t.loadDataSource();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    loadDataSource: function() {
        var t = this;
        wx.showLoading({
            mask: !0
        });
        var e = wx.getStorageSync("userId"), s = {
            userId: e || "",
            id: t.data.taskId
        };
        wx.request({
            url: a.urlList.API_TASK_GET_DETAIL,
            method: "GET",
            data: s,
            header: {
                contentType: "application/json"
            },
            success: function(a) {
                wx.hideLoading(), 200 == a.data.status ? t.setData({
                    taskDetail: a.data.data,
                    isFavorite: a.data.data.task.isFavorite
                }) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        });
    },
    sign: function() {
        var t = this, e = wx.getStorageSync("userId");
        if (e) {
            wx.showLoading({
                mask: !0
            });
            var s = {
                userId: e,
                taskId: t.data.taskId
            };
            wx.request({
                url: a.urlList.API_TASK_SIGN_UP,
                method: "POST",
                header: {
                    contentType: "application/json"
                },
                data: s,
                success: function(a) {
                    if (wx.hideLoading(), 200 == a.data.status) {
                        var e = a.data.data.isTaked;
                        "聘" == t.data.taskDetail.task.iconText ? wx.showModal({
                            title: "系统提示",
                            content: "应聘成功，系统将收到您的申请，是否立即前往『任务管理』查看进度详情？",
                            cancelText: "下次再说",
                            confirmText: "立即前往",
                            success: function(a) {
                                a.confirm ? e ? wx.navigateTo({
                                    url: "taskManagement?type=1"
                                }) : wx.navigateTo({
                                    url: "taskManagement"
                                }) : wx.navigateBack();
                            }
                        }) : wx.showModal({
                            title: "系统提示",
                            content: "报名成功，系统将收到您的申请，是否立即前往『任务管理』查看进度详情？",
                            cancelText: "下次再说",
                            confirmText: "立即前往",
                            success: function(a) {
                                a.confirm ? e ? wx.navigateTo({
                                    url: "taskManagement?type=1"
                                }) : wx.navigateTo({
                                    url: "taskManagement"
                                }) : wx.navigateBack();
                            }
                        });
                    } else 415 == a.data.status ? wx.showModal({
                        title: "系统提示",
                        content: a.data.message,
                        cancelText: "下次再说",
                        confirmText: "立即完善",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/editBank"
                            });
                        }
                    }) : 416 == a.data.status ? wx.showModal({
                        title: "系统提示",
                        content: a.data.message,
                        cancelText: "下次再说",
                        confirmText: "实名认证",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/realNameAuthentication"
                            });
                        }
                    }) : wx.showToast({
                        title: a.data.message,
                        icon: "none"
                    });
                }
            });
        } else wx.navigateTo({
            url: "../login/login"
        });
    },
    getShareQrCode: function() {
        var t = this;
        wx.showLoading({
            mask: !0
        }), wx.request({
            url: a.urlList.API_TASK_GET_WX_QR_CODE,
            data: {
                taskId: t.data.taskId
            },
            method: "GET",
            success: function(a) {
                if (200 == a.data.status) {
                    var e = a.data.data;
                    e = e.replace(/[\r\n]/g, "");
                    var s = wx.base64ToArrayBuffer(e), n = wx.getFileSystemManager(), i = wx.env.USER_DATA_PATH + "/tmp_base64str.png";
                    n.writeFile({
                        filePath: i,
                        data: s,
                        encoding: "binary",
                        success: function() {
                            wx.hideLoading(), wx.getImageInfo({
                                src: i,
                                success: function(a) {
                                    wx.hideLoading(), console.log(a), t.setData({
                                        qrImageUrl: a.path,
                                        isShow: !0
                                    });
                                },
                                fail: function(a) {
                                    console.log(a);
                                }
                            });
                        },
                        fail: function(a) {
                            console.log(a);
                        }
                    });
                } else wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        });
    },
    showShareMa: function() {
        var a = this;
        a.data.qrImageUrl ? a.setData({
            isShow: !0
        }) : a.getShareQrCode();
    },
    closeShareMa: function() {
        this.setData({
            isShow: !1
        });
    },
    saveImageToPhotos: function() {
        var a = this, t = wx.createCanvasContext("share", this);
        t.drawImage("/images/bg_share_task2.png", 0, 0, a.data.canvasWidth, a.data.canvasHeight + 20), 
        t.drawImage("/images/bg_share_task3.png", 18, 120, a.data.canvasWidth - 38, a.data.canvasHeight - 100), 
        t.setTextAlign("center"), t.setFillStyle("#000000"), t.setFontSize(14);
        var e = a.data.taskDetail.task.name;
        console.log(e.length), e.length > 13 ? (t.fillText(e.substring(0, 13), a.data.canvasWidth / 2, 170), 
        t.fillText(e.substring(13), a.data.canvasWidth / 2, 190), t.setFillStyle("red"), 
        t.fillText(a.data.taskDetail.task.taskPrice, a.data.canvasWidth / 2, 210)) : (t.fillText(e, a.data.canvasWidth / 2, 170), 
        t.setFillStyle("red"), t.fillText(a.data.taskDetail.task.taskPrice, a.data.canvasWidth / 2, 200)), 
        t.drawImage(a.data.qrImageUrl, (a.data.canvasWidth - 100) / 2, 240, 100, 100), t.setFillStyle("gray"), 
        t.fillText("长按识别", a.data.canvasWidth / 2, 360), t.draw(), wx.canvasToTempFilePath({
            canvasId: "share",
            success: function(a) {
                wx.saveImageToPhotosAlbum({
                    filePath: a.tempFilePath,
                    success: function() {
                        wx.hideLoading(), wx.showToast({
                            title: "保存成功"
                        });
                    },
                    fail: function() {
                        wx.hideLoading(), wx.showToast({
                            title: "保存失败",
                            icon: "none"
                        });
                    }
                });
            }
        }, this);
    },
    favorite: function() {
        var t = this, e = wx.getStorageSync("userId");
        e ? (wx.showLoading({
            mask: !0
        }), wx.request({
            url: t.data.isFavorite ? a.urlList.API_USER_TASK_FAVORITE_CANCEL_FAVORITE : a.urlList.API_USER_TASK_FAVORITE_FAVORITE,
            data: {
                taskId: t.data.taskId,
                userId: e
            },
            method: "POST",
            success: function(a) {
                wx.hideLoading(), 200 == a.data.status ? t.data.isFavorite ? (wx.showToast({
                    title: "取消收藏"
                }), t.setData({
                    isFavorite: !1
                })) : (wx.showToast({
                    title: "收藏成功"
                }), t.setData({
                    isFavorite: !0
                })) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        })) : wx.navigateTo({
            url: "../login/login"
        });
    },
    previewImage: function(a) {
        wx.previewImage({
            current: a.currentTarget.dataset.image,
            urls: a.currentTarget.dataset.images
        });
    }
});